package com.util;

import org.apache.commons.lang.StringUtils;

public class PagingTool {
	private Integer currentPage;
	private Integer lastPage;// the max page number
	private Integer totalNum;// total record numbers
	private Integer start;// start index
	private Integer lastStart;// the last start index
	private Integer pageSize;// one page records numbers

	public PagingTool(Integer totalNum, Integer pageSize, Integer currentPage) {
		this.pageSize = pageSize;
		this.totalNum = totalNum;
		this.setLastPage(totalNum, pageSize);
		this.setLastStart(lastPage, pageSize);
		this.setCurrentPage(currentPage);
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	/**
	 * calculate fields
	 * 
	 * @param currentPage
	 */
	private void setCurrentPage(Integer currentPage) {
		if (currentPage == null) {
			this.start = 0;
			this.currentPage = 1;
		} else {
			this.currentPage = currentPage;
			if (this.currentPage > this.lastPage) {
				//if currentPage bigger than the last page number
				this.currentPage = this.lastPage;
				this.start = this.getLastStart();//set start index to last index
			} else if (this.currentPage < 1) {
				this.start = 0;
				this.currentPage = 1;
			} else {
				this.start = (this.currentPage - 1) * this.pageSize;
			}
		}
	}

	public Integer getLastPage() {
		this.setLastPage(this.totalNum, this.pageSize);
		return lastPage;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	private void setLastPage(Integer totalNum, Integer pageSize) {
		this.lastPage = (totalNum - 1) / pageSize + 1;
	}

	public Integer getTotalNum() {
		return totalNum;
	}

	public Integer getStart() {
		return start;
	}

	public Integer getLastStart() {
		this.setLastStart(this.lastPage, this.pageSize);
		return lastStart;
	}

	private void setLastStart(Integer lastPage, Integer pageSize) {
		this.lastStart = (lastPage - 1) * pageSize;
	}
	
	public String getNavigatorBar()
	{
		StringBuffer bar=new StringBuffer();
		int curpage = this.getCurrentPage();//currentPage
		int pages   = this.getLastPage();
		String urlstart="javascript:pagenation(";
		String urlend=");return false;";
		/**
		 * first page
		 */
		if(curpage == 1){
			bar.append("<span class='prev'><< PREV</span>")
			   .append("<span class='current'>1</span>");
		}else{
			bar.append("<a href='#'  onclick='"+urlstart+(curpage-1)+urlend+"'><< PREV</a>")
			   .append("<a href='#' class='number' onclick='"+urlstart+1+urlend+"'>1</a>");
		}
		/**
		 * end of first page
		 */
		
		if(pages <= 11){
			for(int i =2 ; i<pages;i++){
				if(curpage==i){
					bar.append("<span class='current'>"+i+"</span>");
				}else{
					bar.append("<a href='#' class='number' onclick='"+urlstart+i+urlend+"'>"+i+"</a>");
				}
			}
		}else{//total page >8
			if(curpage <=6){ 						
				for(int i=2;i<=7;i++){
					if(curpage==i){
						bar.append("<span class='current'>"+i+"</span>");
					}else{
						bar.append("<a href='#' class='number' onclick='"+urlstart+i+urlend+"'>"+i+"</a>");
					}
				}
				bar.append("<span>....</span>");
				bar.append("<a href='#' class='number' onclick='"+urlstart+(pages-1)+urlend+"'>"+(pages-1)+"</a>");
			}else if(curpage>=pages-5){  			//if current page bigger than max-5 link with left pages
				bar.append("<a href='#' class='number' onclick='"+urlstart+2+urlend+"'>"+2+"</a>");
				bar.append("<span>....</span>");
				for(int i=pages-5-3;i<=pages-1;i++){
					if(curpage==i){
						bar.append("<span class='current'>"+i+"</span>");
					}else{
						bar.append("<a href='#' class='number' onclick='"+urlstart+i+urlend+"'>"+i+"</a>");
					}
				}
			}else{
				bar.append("<a href='#' class='number' onclick='"+urlstart+2+urlend+"'>"+2+"</a>");
				bar.append("<span>....</span>");
				for(int i=curpage-3;i<=curpage+3;i++){
					if(curpage==i){
						bar.append("<span class='current'>"+i+"</span>");
					}else{
						bar.append("<a href='#' class='number' onclick='"+urlstart+i+urlend+"'>"+i+"</a>");
					}
				}
				bar.append("<span>....</span>");
				bar.append("<a href='#' class='number' onclick='"+urlstart+(pages-1)+urlend+"'>"+(pages-1)+"</a>");
			}
		}
		
		
		//last page
		if(curpage == pages && curpage !=1){
			bar.append("<span class='current'>"+pages+"</span>")
			   .append("<span class='next'>NEXT >></span>");
		}else if(curpage == pages){
			 bar.append("<span class='next'>NEXT >></span>");
		}else{
			bar.append("<a href='#' class='number' onclick='"+urlstart+pages+urlend+"'>"+pages+"</a>")
			   .append("<a href='#'  onclick='"+urlstart+(curpage+1)+urlend+"'>NEXT >></a>");
		}
		String returnString = bar.toString();
		returnString = "<div class='pagination'>"+returnString+"</div>";
		return returnString;
	}
	
	public String getNavigatorBar(String leftImg ,String rightImg){

		StringBuffer bar=new StringBuffer();
		int curpage = this.getCurrentPage();//currentPage
		int pages   = this.getLastPage();
		String urlstart="javascript:pagenation(";
		String urlend=");return false;";
		/**
		 * first page
		 */
		if(curpage == 1){
//			bar.append("<span class='prev'><< PREV</span>")
//			   .append("<span class='current'>1</span>");
			bar.append("<td>");
			bar.append(leftImg);
			bar.append("</td>");
			bar.append("<td class='nav'>");
			bar.append("<span class='current'>1</span>");
		}else{
			bar.append("<td>");
			bar.append("<a href='#'  onclick='"+urlstart+(curpage-1)+urlend+"'>"+leftImg+"</a>");
			bar.append("</td>");
			bar.append("<td class='nav'>");
			bar.append("<a href='#' class='number_style1' onclick='"+urlstart+1+urlend+"'>1</a>");
		}
		/**
		 * end of first page
		 */
		
		if(pages <= 11){
			for(int i =2 ; i<pages;i++){
				if(curpage==i){
					bar.append("<span class='current'>"+i+"</span>");
				}else{
					bar.append("<a href='#' class='number_style1' onclick='"+urlstart+i+urlend+"'>"+i+"</a>");
				}
			}
		}else{//total page >8
			if(curpage <=6){ 						
				for(int i=2;i<=7;i++){
					if(curpage==i){
						bar.append("<span class='current'>"+i+"</span>");
					}else{
						bar.append("<a href='#' class='number_style1' onclick='"+urlstart+i+urlend+"'>"+i+"</a>");
					}
				}
				bar.append("<span>....</span>");
				bar.append("<a href='#' class='number_style1' onclick='"+urlstart+(pages-1)+urlend+"'>"+(pages-1)+"</a>");
			}else if(curpage>=pages-5){  			//if current page bigger than max-5 link with left pages
				bar.append("<a href='#' class='number_style1' onclick='"+urlstart+2+urlend+"'>"+2+"</a>");
				bar.append("<span>....</span>");
				for(int i=pages-5-3;i<=pages-1;i++){
					if(curpage==i){
						bar.append("<span class='current'>"+i+"</span>");
					}else{
						bar.append("<a href='#' class='number_style1' onclick='"+urlstart+i+urlend+"'>"+i+"</a>");
					}
				}
			}else{
				bar.append("<a href='#' class='number_style1' onclick='"+urlstart+2+urlend+"'>"+2+"</a>");
				bar.append("<span>....</span>");
				for(int i=curpage-3;i<=curpage+3;i++){
					if(curpage==i){
						bar.append("<span class='current'>"+i+"</span>");
					}else{
						bar.append("<a href='#' class='number_style1' onclick='"+urlstart+i+urlend+"'>"+i+"</a>");
					}
				}
				bar.append("<span>....</span>");
				bar.append("<a href='#' class='number_style1' onclick='"+urlstart+(pages-1)+urlend+"'>"+(pages-1)+"</a>");
			}
		}
		
		
		
		//last page
		if(curpage == pages && curpage !=1){
//			bar.append("<span class='current'>"+pages+"</span>")
//			   .append("<span class='next'>NEXT >></span>");
			bar.append("<span class='current'>"+pages+"</span>");
			bar.append("</td>");
			bar.append("<td>");
			bar.append(rightImg);
			bar.append("</td>");

		}else if(curpage == pages){
//			 bar.append("<span class='next'>NEXT >></span>");
			bar.append("</td>");
			bar.append("<td>");
			bar.append(rightImg);
			bar.append("</td>");
		}else{
//			bar.append("<a href='#' class='number' onclick='"+urlstart+pages+urlend+"'>"+pages+"</a>")
//			   .append("<a href='#'  onclick='"+urlstart+(curpage+1)+urlend+"'>NEXT >></a>");
			bar.append("<a href='#' class='number_style1' onclick='"+urlstart+pages+urlend+"'>"+pages+"</a>");
			bar.append("</td>");
			bar.append("<td>");
			bar.append("<a href='#'  onclick='"+urlstart+(curpage+1)+urlend+"'>"+rightImg+"</a>");
			bar.append("</td>");
		}
		String returnString = bar.toString();
		returnString = "<div class='foot_next_style1'><table><tr>"+returnString+"</tr></table></div>";
		return returnString;
	}
	
	public String getNavigatorBar(String pagenation, String parameters, String allLink){
		StringBuffer bar=new StringBuffer();
		int curpage = this.getCurrentPage();//currentPage
		int pages   = this.getLastPage();
		String urlstart="javascript:" + pagenation + "(";
		String urlend = parameters == null ? "":",\""+ parameters + "\"" + ");return false;";
			
		String all = "javascript:" + allLink + "(";
		if(urlend.equals(""))
			all= "javascript:" + allLink;
		/**
		 * first page
		 */
		if(curpage == 1){
			
		}else{
			bar.append("<a href='#'  onclick='"+urlstart+(curpage-1)+urlend+"'>PREV</a>").append(" &nbsp;&nbsp;| &nbsp;&nbsp;");
		}
		
		if(StringUtils.isBlank(allLink)){
			
		}else{
			bar.append("<a href='#'  onclick='" + all + urlend + "'>SHOW ALL</a>");
		}
		
		//last page
		if(curpage == pages){
		}else{
			bar.append("&nbsp; &nbsp;|&nbsp;&nbsp;").append("<a href='#'  onclick='"+urlstart+(curpage+1)+urlend+"'>NEXT</a>");
		}
		String returnString = bar.toString();
		return returnString;		
	}
	
	
	
}
